// 封装一个通用的方法，用于实现数据懒加载
import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'

export const useLazyData = (apiFn) => {
  const target = ref(null)
  const result = ref([])
  const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
    if (isIntersecting) {
      apiFn().then((data) => {
        result.value = data.result
        stop()
      })
    }
  })
  return { target, result }
}
